package com.shishuo.cms.action.manage;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shishuo.cms.auth.AuthPassport;
import com.shishuo.cms.constant.SystemConstant;
import com.shishuo.cms.entity.Admin;
import com.shishuo.cms.entity.RoleEntity;
import com.shishuo.cms.entity.vo.JsonVo;
import com.shishuo.cms.exception.AuthException;

@Controller
@RequestMapping("/admin/admin")
public class AdminAction extends AdminBaseAction {
	@InitBinder("admin")
	public void initBinder(WebDataBinder binder) {
		binder.setFieldDefaultPrefix("admin.");
	}

	@AuthPassport
	@RequestMapping(value = "/add.htm", method = RequestMethod.GET)
	public String addUser(ModelMap modelMap) throws Exception {
		modelMap.put("adminName", "");
		modelMap.put("email", "");
		// 获取所有的角色类型
		List<RoleEntity> roleList = roleService.getAllList();
		modelMap.put("roleList", roleList);
		return "system/admin/add";
	}

	@AuthPassport
	@ResponseBody
	@RequestMapping(value = "/addNew.json", method = RequestMethod.POST)
	public JsonVo<String> addNewUser(
			@RequestParam(value = "roleId") long roleId,
			@RequestParam(value = "adminName") String adminName,
			@RequestParam(value = "email") String email,
			@RequestParam(value = "password") String password) throws Exception {
		Admin admin = adminService.getAdminByEmail(email);
		JsonVo<String> json = new JsonVo<String>();
		if (adminName.equals("")) {
			json.getErrors().put("adminName", "管理员名称不能为空");
		}
		if (email.equals("")) {
			json.getErrors().put("email", "管理员邮箱不能为空");
		} else {
			if (admin != null) {
				json.getErrors().put("email", "管理员邮箱不能重复");
			}
		}
		if (StringUtils.isBlank(password)) {
			json.getErrors().put("password", "管理员密码不能为空");
		} else if (password.length() < 6) {
			json.getErrors().put("password", "密码不能小于6位");
		} else if (password.length() > 16) {
			json.getErrors().put("password", "密码不能大于16位");
		}
		// 检测校验结果
		if (validateJsonIsError(json))
			return json;
		Admin entity = new Admin();
		entity.setCreateTime(new Date());
		entity.setEmail(email);
		entity.setName(adminName);
		entity.setPassword(password);
		entity.setRoleId(roleId);
		entity.setStatus("1");
		adminService.save(entity);
		json.setResult(true);
		return json;
	}

	/**
	 * 进入管理员列表页面
	 * 
	 * @throws Exception
	 * 
	 */
	@AuthPassport
	@RequestMapping(value = "/page.htm", method = RequestMethod.GET)
	public String allList(
			@RequestParam(value = "p", defaultValue = "1") int pageNum,
			ModelMap modelMap) throws Exception {
		modelMap.put("pageVo", adminService.getAllListPage(pageNum));
		return "system/admin/all";
	}

	@AuthPassport
	@RequestMapping(value = "/update.htm", method = RequestMethod.GET)
	public String update(
			@RequestParam(value = "adminId", defaultValue = "0") long adminId,
			ModelMap modelMap, HttpServletRequest request) throws Exception {
		if (0 == adminId) {
			HttpSession session = request.getSession();
			Admin admin = (session == null) ? null : (Admin) session
					.getAttribute(SystemConstant.SESSION_ADMIN);
			if (null != admin) {
				adminId = admin.getAdminId();
			}
		}
		// 获取所有的角色类型
		List<RoleEntity> roleList = roleService.getAllList();
		modelMap.put("roleList", roleList);
		Admin admin = adminService.getById(adminId);
		modelMap.put("admin", admin);
		return "system/admin/update";
	}

	/**
	 * 删除管理员
	 * 
	 */
	@AuthPassport
	@ResponseBody
	@RequestMapping(value = "/delete.json", method = RequestMethod.GET)
	public JsonVo<String> deleteAdmin(
			@RequestParam(value = "adminId") long adminId) throws Exception {
		// throw new AuthException(this.getMsgByMsgKey("system.noLogin"));
		JsonVo<String> json = new JsonVo<String>();
		adminService.deleteById(adminId);
		json.setResult(true);
		return json;
	}

	/**
	 * 修改管理员
	 * 
	 */
	@AuthPassport
	@ResponseBody
	@RequestMapping(value = "/update.json", method = RequestMethod.POST)
	public JsonVo<String> editAdmin(@ModelAttribute Admin admin)
			throws Exception {
		JsonVo<String> json = new JsonVo<String>();
		Admin adminOld = adminService.getById(admin.getAdminId());
		adminOld.setPassword(admin.getPassword());
		adminOld.setName(admin.getName());
		adminService.update(adminOld);
		json.setResult(true);
		return json;
	}
}
